﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using YArchitech.Controls;
using YArchitech.DataModel;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;

namespace YArchitech.HVAC
{
	public partial class SlopeForm : System.Windows.Forms.Form
	{
		public SlopeForm(UserSetting userSetting)
		{
			this.InitializeComponent();
			new YJKProcessTools().ShowInTaskbarChange(this);
			this.userSettingS = userSetting;
		}

		private void btnOk_Click(object sender, EventArgs e)
		{
			if (this.rdoCalculateElevation.Checked)
			{
				this.iSlopeorElevation = 0;
				if (this.txtSlope.Text.Trim() == "")
				{
					MessageBox.Show("参数不能为空", "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
					return;
				}
				this.slope = double.Parse(this.txtSlope.Text);
			}
			else if (this.rdoCalculateSlope.Checked)
			{
				this.iSlopeorElevation = 1;
				if (this.rboStart.Checked)
				{
					this.iStartorEnd = 0;
				}
				else
				{
					this.iStartorEnd = 1;
				}
				if (this.txtElevation.Text.Trim() == "")
				{
					MessageBox.Show("参数不能为空", "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
					return;
				}
				this.elevation = double.Parse(this.txtElevation.Text);
			}
			this.WriteParameterSetting(this.iSlopeorElevation, this.iStartorEnd);
			base.DialogResult = DialogResult.OK;
		}

		private void ReadParameterSetting()
		{
			this.txtSlope.Text = this.userSettingS.GetValue(this.xmlbootname, "slope");
			this.txtElevation.Text = this.userSettingS.GetValue(this.xmlbootname, "elevation");
			this.iSlopeorElevation = int.Parse(this.userSettingS.GetValue(this.xmlbootname, "slopeorElevation"));
			this.iStartorEnd = int.Parse(this.userSettingS.GetValue(this.xmlbootname, "startorEnd"));
			if (this.iSlopeorElevation == 0)
			{
				this.rdoCalculateElevation.Checked = true;
				this.SlopeGroup(false);
			}
			else
			{
				this.rdoCalculateSlope.Checked = true;
				this.ElevationGroup(false);
			}
			if (this.iStartorEnd == 0)
			{
				this.rboStart.Checked = true;
				return;
			}
			this.rboEnd.Checked = true;
		}

		private void WriteParameterSetting(int slopeorElevation, int startorEnd)
		{
			this.userSettingS.WriteValue(this.xmlbootname, "slope", this.txtSlope.Text.Trim());
			this.userSettingS.WriteValue(this.xmlbootname, "elevation", this.txtElevation.Text.Trim());
			this.userSettingS.WriteValue(this.xmlbootname, "slopeorElevation", this.iSlopeorElevation.ToString());
			this.userSettingS.WriteValue(this.xmlbootname, "startorEnd", this.iStartorEnd.ToString());
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			base.Close();
		}

		private void SlopeForm_Load(object sender, EventArgs e)
		{
			this.ReadParameterSetting();
		}

		private void ElevationGroup(bool check)
		{
			this.txtSlope.Enabled = check;
		}

		private void SlopeGroup(bool check)
		{
			this.rboStart.Enabled = check;
			this.rboEnd.Enabled = check;
			this.txtElevation.Enabled = check;
		}

		private void rdoCalculateElevation_CheckedChanged(object sender, EventArgs e)
		{
			if (this.rdoCalculateElevation.Checked)
			{
				this.rdoCalculateSlope.Checked = false;
				this.ElevationGroup(true);
				return;
			}
			this.ElevationGroup(false);
		}

		private void rdoCalculateSlope_CheckedChanged(object sender, EventArgs e)
		{
			if (this.rdoCalculateSlope.Checked)
			{
				this.rdoCalculateElevation.Checked = false;
				this.SlopeGroup(true);
				return;
			}
			this.SlopeGroup(false);
		}

		private void txtSlope_KeyPress(object sender, KeyPressEventArgs e)
		{
			SlopeForm.OnlyEnterNumber(this.txtSlope, e);
		}

		private void txtElevation_KeyPress(object sender, KeyPressEventArgs e)
		{
			SlopeForm.OnlyEnterNumber(this.txtElevation, e);
		}

		public static void OnlyEnterNumber(System.Windows.Forms.TextBox txt, KeyPressEventArgs e)
		{
			if ((e.KeyChar < '0' || e.KeyChar > '9') && e.KeyChar != '\b' && e.KeyChar != '\r' && e.KeyChar != '-' && e.KeyChar != '.')
			{
				e.Handled = true;
			}
			if (e.KeyChar == '-' && (txt.SelectionStart != 0 || txt.Text.IndexOf("-") >= 0))
			{
				e.Handled = true;
			}
			if (e.KeyChar == '.' && txt.Text.IndexOf(".") >= 0)
			{
				e.Handled = true;
			}
		}

		private UserSetting userSettingS;

		private string xmlbootname = "AdjustSlope";

		public double slope;

		public double elevation;

		public int iStartorEnd;

		public int iSlopeorElevation;
	}
}
